package com.data.modules.smzh.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.vo.LoginUser;
import com.data.modules.smzh.entity.SmUser;
import com.data.modules.smzh.service.ISmUserService;
import com.data.modules.smzh.vo.SmUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: user
 * @Author: data-boot
 * @Date:   2022-11-27
 * @Version: V1.0
 */
@Api(tags="user")
@RestController
@RequestMapping("/smzh/user")
@Slf4j
public class UserController extends BootController<SmUser, ISmUserService> {
	@Autowired
	private ISmUserService userService;

	/**
	 * 分页列表查询
	 *
	 * @param user
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	/*@AutoLog(value = "user-分页列表查询")
	@ApiOperation(value="user-分页列表查询", notes="user-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmUser user,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmUser> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());
		Page<SmUser> page = new Page<SmUser>(pageNo, pageSize);
		IPage<SmUser> pageList = userService.page(page, queryWrapper);
		return Result.OK(pageList);
	}*/

	 /**
	  * 分页列表查询
	  *
	  * @param user
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "user-分页列表查询")
	 @ApiOperation(value="user-分页列表查询", notes="user-分页列表查询")
	 //@GetMapping(value = "/findUserByPage")
	 @GetMapping(value = "/list")
	 public Result<?> queryUserPageList(SmUserVo user,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 Page<SmUserVo> page = new Page<SmUserVo>(pageNo, pageSize);
		// IPage<SmUser> pageList = userService.page(page, queryWrapper);
		 //使用多线程查询 建筑 人脸 基本信息
		 //todo

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 user.setAppId(Integer.valueOf(sysUser.getId()));


		 IPage<SmUserVo> pageList= userService.pageList(page, user);

		 return Result.OK(pageList);
	 }


	 @AutoLog(value = "user-分页列表查询")
	 @ApiOperation(value="user-分页列表查询", notes="user-分页列表查询")
	 //@GetMapping(value = "/findUserByPage")
	 @GetMapping(value = "/findAllBindUserByPage")
	 public Result<?> queryAllBindUserByPage(SmUserVo user,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										HttpServletRequest req) {
		 Page<SmUserVo> page = new Page<SmUserVo>(pageNo, pageSize);
		 // IPage<SmUser> pageList = userService.page(page, queryWrapper);
		 //使用多线程查询 建筑 人脸 基本信息
		 //todo
		 IPage<SmUserVo> pageList = null;
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 user.setAppId(Integer.valueOf(sysUser.getId()));
		 if(ObjectUtil.isNotEmpty(user.getUniqueNo())){

		 	 //调整长度
			 user.setUniqueNo(user.getAppId()+user.getUniqueNo());
			 user.setUniqueNoLengh(user.getUniqueNo().length());
			 pageList= userService.queryBindUserPageList(page, user);
		 }else{

		 	//设备编号
		 	if(ObjectUtil.isNotEmpty(user.getSerialNo())){

				pageList= userService.queryBindUserBySerNoPageList(page, user);
			}
		 }




		 return Result.OK(pageList);
	 }

	 /**
	 *   添加
	 *
	 * @param user
	 * @return
	 */
	@AutoLog(value = "user-添加")
	@ApiOperation(value="user-添加", notes="user-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SmUser user) {
		userService.save(user);
		return Result.OK("添加成功！");
	}


	 @AutoLog(value = "user-添加")
	 @ApiOperation(value="user-添加", notes="user-添加")
	 @PostMapping(value = "/addUser")
	 public Result<?> addUser(@RequestBody SmUserVo user) {

		 userService.addUserInfo(user);

		 //userService.save(user);
		 return Result.OK("添加成功！");
	 }

	 /**
	 *  编辑
	 *
	 * @param user
	 * @return
	 */
	@AutoLog(value = "user-编辑")
	@ApiOperation(value="user-编辑", notes="user-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SmUser user) {
		userService.updateById(user);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "user-通过id删除")
	@ApiOperation(value="user-通过id删除", notes="user-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		userService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "user-批量删除")
	@ApiOperation(value="user-批量删除", notes="user-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.userService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "user-通过id查询")
	@ApiOperation(value="user-通过id查询", notes="user-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        SmUser user = userService.getById(id);
		if(user==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(user);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param user
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmUser user) {
        return super.exportXls(request, user, SmUser.class, "SmUser");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmUser.class);
    }

}
